package fr.umlv.utils;

import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;

import fr.umlv.authentication.SessionHandler;

// TODO: Auto-generated Javadoc
/**
 * The Class ExceptionUtils.
 */
public class ExceptionUtils {
	
	/** The bundle. */
	private static ResourceBundle bundle = ResourceBundle.getBundle("kiyut.ekspos.BrowserWindow", SessionHandler.getLocale());
	
	/** The Constant errorsMapText. */
	private static final HashMap<String, String> errorsMapText = new HashMap<String, String>();
	
	/** The Constant errorsMapTitle. */
	private static final HashMap<String, String> errorsMapTitle = new HashMap<String, String>();
	
	/** The Constant MSG_PATTERN_EXCEPTION. */
	private static final Pattern MSG_PATTERN_EXCEPTION = Pattern.compile("<tr><td><b>Message:</b></td><td>(.*)</td></tr>");
	
	/** The Constant PATTERN_EXCEPTION. */
	private static final Pattern PATTERN_EXCEPTION = Pattern.compile("<tr><td><b>Exception:</b></td><td>(.*)</td></tr>");
	
	static {
		errorsMapTitle.put("Permission", bundle.getString("exception.accessDenied.title"));
		errorsMapTitle.put("AccessDeniedException", bundle.getString("exception.accessDenied.title"));
		errorsMapTitle.put("FileExistsException", bundle.getString("exception.alreadyexists.title"));
		errorsMapTitle.put("CMISVersioningException", bundle.getString("exception.versionning.title"));
		
		errorsMapText.put("Permission", bundle.getString("exception.accessDenied.text"));
		errorsMapText.put("AccessDeniedException", bundle.getString("exception.accessDenied.text"));
		errorsMapText.put("FileExistsException", bundle.getString("exception.alreadyexists.text"));
		errorsMapText.put("CMISVersioningException", bundle.getString("exception.versionning.text"));
		
	}
	
	
	/**
	 * Creates the cmis jx permission denied error exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createCmisJXPermissionDeniedErrorException(CmisPermissionDeniedException e, final Component parentComponent) {
		final ErrorInfo info = new ErrorInfo(bundle.getString("exception.accessDenied.title"), bundle.getString("exception.accessDenied.text"), e.getErrorContent(), "CmisPermissionDeniedException", e, Level.WARNING, null);
		JXErrorPane.showDialog(parentComponent, info);
	}

	/**
	 * Creates the cmis jx cmis object not found exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createCmisJXCmisObjectNotFoundException(CmisObjectNotFoundException e, final Component parentComponent) {
		final ErrorInfo info = new ErrorInfo(bundle.getString("exception.notfound.title"), bundle.getString("exception.notfound.text"), e.getErrorContent(), "CmisPermissionDeniedException", e, Level.WARNING, null);
		JXErrorPane.showDialog(parentComponent, info);
	}

	
	/**
	 * Creates the cmis jx base exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createCmisJXBaseException(CmisBaseException e, final Component parentComponent) {
		String errorContent = e.getErrorContent();
		String titleError = e.getMessage();
		String textError = e.getMessage();
		ErrorInfo info = new ErrorInfo(titleError, textError, errorContent, "CmisBaseException", e, Level.SEVERE, null);
		
		try {
			Matcher m = MSG_PATTERN_EXCEPTION.matcher(errorContent);
			if (m.find()) {
				titleError = e.getMessage();
				textError = m.group(1);
				for(Map.Entry<String, String> errors:errorsMapTitle.entrySet())  {
					String errorKey = errors.getKey();
					if (textError.contains(errorKey)) {
						titleError = errorsMapTitle.get(errorKey);
						textError = errorsMapText.get(errorKey);
					}
				}
				info = new ErrorInfo(titleError, textError, errorContent, "CmisBaseException", e, Level.SEVERE, null);
			}
		} catch (NullPointerException npe) {}
		
		try {
			Matcher m2 = PATTERN_EXCEPTION.matcher(errorContent);
			if (m2.find()) {
				textError = m2.group(1);
				if (textError!=null) {
					for(Map.Entry<String, String> errors:errorsMapTitle.entrySet())  {
						String errorKey = errors.getKey();
						if (textError.contains(errorKey)) {
							titleError = errorsMapTitle.get(errorKey);
							textError = errorsMapText.get(errorKey);
						}
					}
					info = new ErrorInfo(titleError, textError, errorContent, "CmisBaseException", e, Level.SEVERE, null);
				}
			}
		} catch (NullPointerException npe) {}

		JXErrorPane.showDialog(parentComponent, info);

	}
	
	
	/**
	 * Creates the jx error exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createJXErrorException(Exception e, final Component parentComponent) {
		final ErrorInfo info = new ErrorInfo(e.getMessage(), e.getMessage(), null, "Exception", e, Level.SEVERE, null);
		JXErrorPane.showDialog(parentComponent, info);
	}
	
	/**
	 * Creates the jx error io exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createJXErrorIOException(IOException e, final Component parentComponent) {
		final ErrorInfo info = new ErrorInfo(bundle.getString("exception.io.title"), bundle.getString("exception.io.text"), null, "IOException", e, Level.WARNING, null);
		JXErrorPane.showDialog(parentComponent, info);
	}
	
	/**
	 * Creates the jx file not found exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createJXFileNotFoundException(FileNotFoundException e, final Component parentComponent) {
		final ErrorInfo info = new ErrorInfo(bundle.getString("exception.iofile.title"), bundle.getString("exception.iofile.text"), null, "FileNotFoundException", e, Level.WARNING, null);
		JXErrorPane.showDialog(parentComponent, info);
	}
	
	
	/**
	 * Creates the jx error interrupted io exception.
	 *
	 * @param e the e
	 * @param parentComponent the parent component
	 */
	public static void createJXErrorInterruptedIOException(InterruptedIOException e, final Component parentComponent) {
		final ErrorInfo info = new ErrorInfo(bundle.getString("exception.interrupted.title"), bundle.getString("exception.interrupted.text"), null, "InterruptedIOException", e, Level.INFO, null);
		JXErrorPane.showDialog(parentComponent, info);
	}
}
